/**
 * Copyright(c) 2013 Jade Techonologies Co., Ltd.
 *
 * History:
 *   13-4-28 上午8:56 Created by dwzhang
 */
package com.jade.framework.base.context;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

import com.jade.framework.base.JadeSystem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.util.CollectionUtils;

/**
 * 扩展的属性配置器。
 *
 * @author dwzhang
 * @version 1.0 13-4-28 上午8:56
 */
public class JadePropertyConfigurer extends PropertyPlaceholderConfigurer
{
    @Override
    protected Properties mergeProperties()
            throws IOException
    {
        Properties props = super.mergeProperties();
        //合并启动路径中带的配置文件
        String propFiles = JadeSystem.getProperty("jade.props");
        if (StringUtils.isNotBlank(propFiles)) {
            String[] files = StringUtils.split(propFiles, ',');
            for (String filepath : files) {
                try {
                    Resource resource = null;
                    File file = new File(filepath);
                    if (file.exists()) {
                        resource = new FileSystemResource(file);
                    }
                    else {
                        resource = new ClassPathResource(filepath);
                    }
                    EncodedResource encodedResource = new EncodedResource(resource, "UTF-8");
                    CollectionUtils.mergePropertiesIntoMap(PropertiesLoaderUtils.loadProperties(encodedResource),
                                                           props);
                }
                catch (Throwable t) {
                    JadeSystem.getLog().warn("Can't parse config file " + filepath, t);
                }
            }
        }
        return props;
    }

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
            throws BeansException
    {
        super.processProperties(beanFactoryToProcess, props);
        for (Enumeration propertyNames = props.propertyNames(); propertyNames.hasMoreElements(); ) {
            String propertyName = (String)propertyNames.nextElement();
            String propertyValue = props.getProperty(propertyName);
            //已经转换过了
            //String convertedValue = convertProperty(propertyName, propertyValue);
            JadeSystem.getLog().debug("Set property " + propertyName + "to " + propertyValue);
            JadeSystem.setProperty(propertyName, propertyValue);
        }
    }
}
